Page({
  data: {
    technicianId: '',
    serviceId: '',
    service: {
      id: 1,
      name: '全身放松按摩',
      image: '/assets/images/service1.jpg',
      price: 198
    },
    technician: {
      id: 1,
      name: '张技师'
    },
    appointmentDate: '',
    appointmentTime: '14:00',
    minDate: '',
    maxDate: '',
    addresses: [
      {
        id: 1,
        contact: '李先生',
        phone: '13800138000',
        address: '北京市朝阳区某某街道某某小区1号楼101室',
        isDefault: true
      }
    ],
    selectedAddressId: 1,
    contactPerson: '李先生',
    contactPhone: '13800138000',
    remarks: ''
  },

  onLoad: function(options) {
    const now = new Date();
    const minDate = this.formatDate(now);
    const maxDate = this.formatDate(new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000)); // 30天后
    
    this.setData({
      technicianId: options.technicianId || '',
      serviceId: options.serviceId || '',
      appointmentDate: minDate,
      minDate: minDate,
      maxDate: maxDate
    });
    
    // 加载服务和技师信息
    this.loadServiceInfo();
    this.loadTechnicianInfo();
  },

  formatDate: function(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  loadServiceInfo: function() {
    // 模拟加载服务信息
    // 实际项目中应该调用后端接口获取真实数据
  },

  loadTechnicianInfo: function() {
    // 模拟加载技师信息
    // 实际项目中应该调用后端接口获取真实数据
  },

  onDateChange: function(e) {
    this.setData({
      appointmentDate: e.detail.value
    });
  },

  onTimeChange: function(e) {
    this.setData({
      appointmentTime: e.detail.value
    });
  },

  selectAddress: function(e) {
    const addressId = e.currentTarget.dataset.id;
    this.setData({
      selectedAddressId: addressId
    });
    
    // 更新联系人信息
    const selectedAddress = this.data.addresses.find(addr => addr.id == addressId);
    if (selectedAddress) {
      this.setData({
        contactPerson: selectedAddress.contact,
        contactPhone: selectedAddress.phone
      });
    }
  },

  editAddress: function(e) {
    const addressId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/customer/addressEdit?id=${addressId}`
    });
  },

  addAddress: function() {
    wx.navigateTo({
      url: '/pages/customer/addressEdit'
    });
  },

  onContactPersonChange: function(e) {
    this.setData({
      contactPerson: e.detail.value
    });
  },

  onContactPhoneChange: function(e) {
    this.setData({
      contactPhone: e.detail.value
    });
  },

  onRemarksChange: function(e) {
    this.setData({
      remarks: e.detail.value
    });
  },

  submitOrder: function() {
    // 表单验证
    if (!this.data.contactPerson) {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.contactPhone) {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      });
      return;
    }
    
    // 提交订单
    const app = getApp();
    const selectedAddress = this.data.addresses.find(addr => addr.id == this.data.selectedAddressId);
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/create/`,
      method: 'POST',
      data: {
        technician: this.data.technicianId,
        service: this.data.serviceId,
        appointment_time: `${this.data.appointmentDate} ${this.data.appointmentTime}`,
        address: selectedAddress ? selectedAddress.address : '',
        contact_person: this.data.contactPerson,
        contact_phone: this.data.contactPhone,
        remarks: this.data.remarks
      },
      success: (res) => {
        if (res.statusCode === 201) {
          // 跳转到订单详情页面
          wx.navigateTo({
            url: `/pages/customer/orderDetail?id=${res.data.id}`
          });
        } else {
          wx.showToast({
            title: '提交订单失败',
            icon: 'none'
          });
        }
      }
    });
  }
})